<?php

/**
 * This is the model class for table "{{kelompok}}".
 *
 * The followings are the available columns in table '{{kelompok}}':
 * @property integer $id_kelompok
 * @property string $nama_kelompok
 * @property string $keterangan
 * @property integer $id_kabupaten
 * @property integer $ketua
 * @property integer $wakil_ketua
 * @property integer $imam
 * @property integer $wakil_imam_1
 * @property integer $wakil_imam_2
 *
 * The followings are the available model relations:
 * @property Jamaah[] $jamaahs
 * @property Kegiatan[] $kegiatans
 * @property Jamaah $wakilKetua
 * @property Jamaah $imam0
 * @property Jamaah $wakilImam1
 * @property Jamaah $wakilImam2
 * @property Kabupaten $idKabupaten
 * @property Jamaah $ketua0
 * @property integer $jenis_kelamin
 */
class Kelompok extends CActiveRecord {

    public $nama_kabupaten;
    public $nama;
    public $kode_lokasi;

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{kelompok}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('id_kabupaten, ketua, wakil_ketua, imam, wakil_imam_1, wakil_imam_2,jenis_kelamin',
                'numerical', 'integerOnly' => true),
            array('nama_kelompok', 'length', 'max' => 255),
            array('keterangan', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id_kelompok, nama_kelompok, keterangan, id_kabupaten, ketua, wakil_ketua, imam, wakil_imam_1, wakil_imam_2, nama_kabupaten,jenis_kelamin',
                'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'jamaahs' => array(self::HAS_MANY, 'Jamaah', 'id_kelompok'),
            'kegiatans' => array(self::HAS_MANY, 'Kegiatan', 'id_kelompok'),
            'wakilKetua' => array(self::BELONGS_TO, 'Jamaah', 'wakil_ketua'),
            'imam0' => array(self::BELONGS_TO, 'Jamaah', 'imam'),
            'wakilImam1' => array(self::BELONGS_TO, 'Jamaah', 'wakil_imam_1'),
            'wakilImam2' => array(self::BELONGS_TO, 'Jamaah', 'wakil_imam_2'),
            'idKabupaten' => array(self::BELONGS_TO, 'Kabupaten', 'id_kabupaten'),
            'ketua0' => array(self::BELONGS_TO, 'Jamaah', 'ketua'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id_kelompok' => 'Id Kelompok',
            'nama_kelompok' => 'Nama Kelompok',
            'keterangan' => 'Keterangan',
            'id_kabupaten' => 'Id Kabupaten',
            'ketua' => 'Ketua',
            'wakil_ketua' => 'Wakil Ketua',
            'imam' => 'Imam',
            'wakil_imam_1' => 'Wakil Imam 1',
            'wakil_imam_2' => 'Wakil Imam 2',
            'jenis_kelamin' => 'Jenis Kelamin',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id_kelompok', $this->id_kelompok);
        $criteria->compare('nama_kelompok', $this->nama_kelompok, true);
        $criteria->compare('keterangan', $this->keterangan, true);
        $criteria->compare('t.id_kabupaten', $this->id_kabupaten);
        $criteria->compare('ketua', $this->ketua);
        $criteria->compare('wakil_ketua', $this->wakil_ketua);
        $criteria->compare('imam', $this->imam);
        $criteria->compare('wakil_imam_1', $this->wakil_imam_1);
        $criteria->compare('wakil_imam_2', $this->wakil_imam_2);
        $criteria->compare('jenis_kelamin', $this->jenis_kelamin);
        //   $criteria->with = array('ketua0');
//        $criteria->compare('ketua0.nama', $this->$nama, true);

        $criteria->with = array('idKabupaten');
        $criteria->compare('idKabupaten.nama_kabupaten', $this->nama_kabupaten,
                true);
        $criteria->compare('idKabupaten.kode_lokasi', $this->kode_lokasi, true);

        return new CActiveDataProvider($this,
                array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Kelompok the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

}
